<template>
  <div class="add-node-btn-box">
    <div class="add-node-btn">
      <a-popover position="right">
        <a-button type="primary" shape="circle">
          <icon-plus />
        </a-button>
        <template #content>
          <div class="add-node-popover-body">
            <a class="add-node-popover-item approver" @click="addType(1)">
              <div class="item-wrapper">
                <span class="iconfont"><icon-user /></span>
              </div>
              <p>审批人</p>
            </a>
            <a class="add-node-popover-item notifier" @click="addType(2)">
              <div class="item-wrapper">
                <span class="iconfont"><icon-send /></span>
              </div>
              <p>抄送人</p>
            </a>
            <a class="add-node-popover-item condition" @click="addType(4)">
              <div class="item-wrapper">
                <span class="iconfont"><icon-branch /></span>
              </div>
              <p>条件分支</p>
            </a>
          </div>
        </template>
      </a-popover>
    </div>
  </div>
</template>

<script setup lang="ts">
import { FlowPanelPoint } from '@/apis/flowPanelPoint/type';
import { ref } from 'vue';

let props = defineProps({
  childNodeP: {
    type: Object,
    default: () => ({})
  },
  id: {
    type: Number
  },
  type: {
    type: Number //3汇合节点
  }
});

let emits = defineEmits(['update:childNodeP', 'addNode']);
const addType = (type: number) => {
  // 节点类型（1 审批节点、2 抄送节点、3 子级流程、4 条件分支、5 发起人节点 6 结束节点）
  let data: FlowPanelPoint;
  if (type === 1) {
    data = {
      pointName: '审核人',
      pointType: 1,
      examineType: 1,
      participantIsNull: 1,
      participantFormat: 1
    };
  } else if (type === 2) {
    data = {
      pointName: '抄送人',
      pointType: 2,
      examineType: 1
    };
  } else {
    data = {
      pointName: '条件',
      pointType: 4,
      priority: 1,
      conditionType: 1
    };
  }
  //添加汇合点
  if (props.type === 3) {
    //获取该节点下一个非条件节点nextNode,然后获取nextNode的关联
    //nextNode的id为新增数据的nextStep，
    //nextNode的关联数据的previousStep为新增数据的previousStep
    console.log('props.childNodeP', props.childNodeP);

    const { childNode } = props.childNodeP;
    if (!childNode) return;
    data.processId = props.childNodeP.processId;
    data.relDescDetail = [];
    childNode.relDescDetail?.forEach((element) => {
      const { nextStep, nextStepType, previousStep, previousStepType } =
        element;
      data.relDescDetail?.push({
        nextStep: childNode.id,
        nextStepType: childNode.pointType,
        previousStep,
        previousStepType
      });
    });
  } else {
    data.processId = props.childNodeP.processId;
    data.relDescDetail = [];
    props.childNodeP.relDescDetail?.forEach((element) => {
      const { nextStep, nextStepType, previousStep, previousStepType } =
        element;
      const index = data.relDescDetail?.findIndex(
        (item) =>
          item.nextStep === nextStep &&
          item.previousStep === props.childNodeP.id
      );
      if (index === -1) {
        data.relDescDetail?.push({
          nextStep,
          nextStepType,
          previousStep: props.childNodeP.id,
          previousStepType: props.childNodeP.pointType
        });
      }
    });
  }

  emits('addNode', data);
};
</script>

<style scoped lang="less">
.add-node-btn-box {
  width: 240px;
  display: -webkit-inline-box;
  display: -ms-inline-flexbox;
  display: inline-flex;
  -ms-flex-negative: 0;
  flex-shrink: 0;
  -webkit-box-flex: 1;
  -ms-flex-positive: 1;
  position: relative;

  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: -1;
    margin: auto;
    width: 2px;
    height: 100%;
    background-color: #cacaca;
  }

  .add-node-btn {
    user-select: none;
    width: 240px;
    padding: 20px 0 32px;
    display: flex;
    -webkit-box-pack: center;
    justify-content: center;
    flex-shrink: 0;
    -webkit-box-flex: 1;
    flex-grow: 1;

    .btn {
      outline: none;
      box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
      width: 30px;
      height: 30px;
      background: #3296fa;
      border-radius: 50%;
      position: relative;
      border: none;
      line-height: 30px;
      -webkit-transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
      transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);

      .iconfont {
        color: #fff;
        font-size: 16px;
      }

      &:hover {
        transform: scale(1.3);
        box-shadow: 0 13px 27px 0 rgba(0, 0, 0, 0.1);
      }

      &:active {
        transform: none;
        background: #1e83e9;
        box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
      }
    }
  }
}
</style>
<style lang="less">
.add-node-popover-body {
  display: flex;

  .add-node-popover-item {
    margin-right: 10px;
    cursor: pointer;
    text-align: center;
    flex: 1;
    color: #191f25 !important;

    .item-wrapper {
      user-select: none;
      display: inline-block;
      width: 80px;
      height: 80px;
      margin-bottom: 5px;
      background: #fff;
      border: 1px solid #e2e2e2;
      border-radius: 50%;
      transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);

      .iconfont {
        font-size: 35px;
        line-height: 80px;
      }
    }

    &.approver {
      .item-wrapper {
        color: #ff943e;
      }
    }

    &.notifier {
      .item-wrapper {
        color: #3296fa;
      }
    }

    &.condition {
      .item-wrapper {
        color: #15bc83;
      }
    }

    &:hover {
      .item-wrapper {
        background: #3296fa;
        box-shadow: 0 10px 20px 0 rgba(50, 150, 250, 0.4);
      }

      .iconfont {
        color: #fff;
      }
    }

    &:active {
      .item-wrapper {
        box-shadow: none;
        background: #eaeaea;
      }

      .iconfont {
        color: inherit;
      }
    }
  }
}
</style>
